//
//  V4LicensePlateOtherTypeView.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/8/1.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "V4LicensePlateOtherTypeView.h"

@interface V4LicensePlateOtherTypeView (){    //
    NSArray *arraySort;
    NSMutableArray *arraylevel;
    UIView *view_userContact;
    UIScrollView *scrollBG;
}

@end

@implementation V4LicensePlateOtherTypeView

-(instancetype)init{
    self=[super init];
    if (self) {
        
        self.frame=[UIScreen mainScreen].bounds;
        
        self.backgroundColor=[UIColor clearColor];
        _floatTypeY=104;
        
        UIControl *closeC=[[UIControl alloc]initWithFrame:self.bounds];
        [closeC addTarget:self action:@selector(closeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:closeC];
        
        view_userContact=[[UIView alloc] initWithFrame:CGRectMake(0, _floatTypeY, kScreenWidth, kScreenHeight-_floatTypeY)];
        [view_userContact setBackgroundColor:RGBACOLOR(50, 50, 50, 0.6)];
        [self addSubview:view_userContact];
        UIControl *closeBG=[[UIControl alloc]initWithFrame:view_userContact.bounds];
        [closeBG addTarget:self action:@selector(closeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [view_userContact addSubview:closeBG];
        
        scrollBG=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
        scrollBG.backgroundColor=[UIColor whiteColor];
        [view_userContact addSubview:scrollBG];
        
        float fy=10;
        //
        UILabel *lblT=[RHMethods labelWithFrame:CGRectMake(10, fy, 0, 20) font:fontTxtContent color:rgbTxtDeepGray text:@"排序"];
        [scrollBG addSubview:lblT];
        fy+=25;
        arraySort=@[@{@"name":@"默认排序"},
                    @{@"name":@"最新",@"id":@"new"},
                    @{@"name":@"浏览最多",@"id":@"hit"}];
        UIView *bgV= [self loadTitlesViewWithData:arraySort withFrame:CGRectMake(10, fy, W(self)-20, 20) withSel:@selector(selectTitleClicekd:) selectTitle:@""];
        [scrollBG addSubview:bgV];
        fy+=H(bgV)+5;
        //
        UILabel *lbllevel=[RHMethods labelWithFrame:CGRectMake(10, fy, 0, 20) font:fontTxtContent color:rgbTxtDeepGray text:@"等级"];
        [scrollBG addSubview:lbllevel];
        fy+=25;
        arraylevel=[[NSMutableArray alloc] initWithObjects:@{@"name":@"所有等级"}, nil];
        [arraylevel addObjectsFromArray:[[[Utility Share] userTypeData] objectForJSONKey:@"level"]];
        UIView *bgLevel= [self loadTitlesViewWithData:arraylevel withFrame:CGRectMake(10, fy, W(self)-20, 20) withSel:@selector(selectlevelClicekd:) selectTitle:@""];
        [scrollBG addSubview:bgLevel];
        fy+=H(bgV)+20;
        //
        scrollBG.frameHeight=fy;
        scrollBG.contentSize=CGSizeMake(kScreenWidth, fy);
    }
    return self;
}

///单选条目
-(UIView *)loadTitlesViewWithData:(NSArray *)arrayTitle withFrame:(CGRect)rect withSel:(SEL)sel selectTitle:(NSString *)selecTitle{
    UIView *viewBG=[[UIView alloc] initWithFrame:rect];
    viewBG.backgroundColor=[UIColor clearColor];
    
    float fmaxW=W(viewBG);
    float fx=0;
    float fy=0;
    NSInteger countT=[arrayTitle count];
    for (int i=0; i<countT; i++) {
        NSDictionary *dic=[arrayTitle objectAtIndex:i];
        UILabel *lblProject=[RHMethods labelWithFrame:CGRectMake(0, 0, 20, 25) font:fontTxtContent color:rgbTitleDeepGray text:[dic valueForJSONStrKey:@"name"] textAlignment:NSTextAlignmentCenter];
        [viewBG addSubview:lblProject];
        CGSize size = [lblProject sizeThatFits:CGSizeMake(MAXFLOAT, 20)];
        float tw=size.width+20;
        if (tw>(fmaxW-fx)) {
            //大于剩余的宽
            if (fx>0) {
                //换一行
                fx=0;
                fy+=25+10;
            }
            tw=tw>(fmaxW)?(fmaxW):tw;
        }
        lblProject.tag=i+10;
        lblProject.frame=CGRectMake(fx, fy, tw, 25);
        if ([selecTitle isEqualToString:[dic valueForJSONStrKey:@"name"]]) {
            lblProject.textColor=rgbpublicColor;
            [[Utility Share] viewLayerRound:lblProject borderWidth:0.5 cornerRadius:2 borderColor:rgbpublicColor];
//            [lblProject viewLayerRoundBorderWidth:0.5 cornerRadius:2 borderColor:rgbpublicColor];
        }else{
            [[Utility Share] viewLayerRound:lblProject borderWidth:0.5 cornerRadius:2 borderColor:rgbLineColor];
//            [lblProject viewLayerRoundBorderWidth:0.5 cornerRadius:2 borderColor:rgbLineColor];
        }
        
        lblProject.userInteractionEnabled=YES;
        [lblProject addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:sel]];
        fx+=tw+10;
    }
    fy+=35;
    viewBG.frameHeight=fy;
    return viewBG;
}

-(void)selectTitleWView:(UIView *)aview{
    UILabel *lblT=(UILabel *)aview;
    for (UIView *view in [lblT.superview subviews]) {
        if (view.tag>=10 && [view isKindOfClass:[UILabel class]]) {
            UILabel *lbl=(UILabel *)view;
            lbl.textColor=rgbTitleDeepGray;
            [[Utility Share] viewLayerRound:lbl borderWidth:0.5 cornerRadius:2 borderColor:rgbLineColor];
//            [lbl viewLayerRoundBorderWidth:0.5 cornerRadius:2 borderColor:rgbLineColor];
        }
    }
    lblT.textColor=rgbpublicColor;
    [[Utility Share] viewLayerRound:lblT borderWidth:0.5 cornerRadius:2 borderColor:rgbpublicColor];
//    [lblT viewLayerRoundBorderWidth:0.5 cornerRadius:2 borderColor:rgbpublicColor];
}

#pragma mark button
-(void)closeButtonClicked{
    [self hidden];
}

-(void)selectTitleClicekd:(UITapGestureRecognizer *)tap{
    [self selectTitleWView:tap.view];
    NSInteger index=tap.view.tag-10;
    NSDictionary *dic=[arraySort objectAtIndex:index];
    self.lpSuc?self.lpSuc(1, dic):nil;
    [self hidden];
}
-(void)selectlevelClicekd:(UITapGestureRecognizer *)tap{
    [self selectTitleWView:tap.view];
    NSInteger index=tap.view.tag-10;
    NSDictionary *dic=[arraylevel objectAtIndex:index];
    self.lpSuc?self.lpSuc(2, dic):nil;
    [self hidden];
}
- (void)show:(LicensePlateSuc)lps
{
    self.lpSuc = lps;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hidden) name:@"push_showLogin" object:nil];
    view_userContact.frame=CGRectMake(X(view_userContact), _floatTypeY, W(view_userContact), kScreenHeight-_floatTypeY);
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    
    self.hidden = NO;
    self.alpha = 0.0f;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0f;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hidden
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.lpSuc?self.lpSuc(0, nil):nil;
    self.lpSuc = nil;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



@end
